home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / NT / CODE / CHAP06 / DLGDEMO4 / DLGDEMO4.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  6.3 KB  |  253 lines

  1. //***********************************************************************
  2. //
  3. //  DlgDemo4.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "DlgDemo4.h"
  10.  
  11. CMyApp myApp;
  12.  
  13. /////////////////////////////////////////////////////////////////////////
  14. // CMyApp member functions
  15.  
  16. BOOL CMyApp::InitInstance ()
  17. {
  18.     m_pMainWnd = new CMainWindow;
  19.     m_pMainWnd->ShowWindow (m_nCmdShow);
  20.     m_pMainWnd->UpdateWindow ();
  21.     return TRUE;
  22. }
  23.  
  24. /////////////////////////////////////////////////////////////////////////
  25. // CMainWindow message map and member functions
  26.  
  27. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  28.     ON_WM_ERASEBKGND ()
  29.     ON_WM_PAINT ()
  30.     ON_COMMAND (IDM_OPTIONS_SETTINGS, OnOptionsSettings)
  31.     ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
  32.     ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
  33. END_MESSAGE_MAP ()
  34.  
  35. CMainWindow::CMainWindow ()
  36. {
  37.     m_nFillType = 1;
  38.     m_nFillColor = 2;
  39.     m_text = "Hello, MFC";
  40.     m_nHeight = 72;
  41.     m_bBold = TRUE;
  42.     m_bItalic = TRUE;
  43.  
  44.     Create (NULL, "DlgDemo4", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
  45.         MAKEINTRESOURCE (IDR_MAINFRAME));
  46. }
  47.  
  48. BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
  49. {
  50.     CRect rect;
  51.     GetClientRect (&rect);
  52.  
  53.     m_nFillType == 1 ? DoGradientFill (pDC, &rect) :
  54.         DoSolidFill (pDC, &rect);
  55.     return TRUE;
  56. }
  57.  
  58. void CMainWindow::OnPaint ()
  59. {
  60.     CRect rect;
  61.     GetClientRect (&rect);
  62.  
  63.     CPaintDC dc (this);
  64.     DoDrawText (&dc, &rect);
  65. }
  66.  
  67. void CMainWindow::OnOptionsSettings ()
  68. {
  69.     CSettingsDialog dlg (this);
  70.  
  71.     dlg.m_nFillType = m_nFillType;
  72.     dlg.m_nFillColor = m_nFillColor;
  73.     dlg.m_text = m_text;
  74.     dlg.m_nHeight = m_nHeight;
  75.     dlg.m_bBold = m_bBold;
  76.     dlg.m_bItalic = m_bItalic;
  77.  
  78.     if (dlg.DoModal () == IDOK) {
  79.         m_nFillType = dlg.m_nFillType;
  80.         m_nFillColor = dlg.m_nFillColor;
  81.         m_text = dlg.m_text;
  82.         m_nHeight = dlg.m_nHeight;
  83.         m_bBold = dlg.m_bBold;
  84.         m_bItalic = dlg.m_bItalic;
  85.  
  86.         Invalidate ();
  87.     }
  88. }
  89.  
  90. void CMainWindow::OnOptionsExit ()
  91. {
  92.     SendMessage (WM_CLOSE, 0, 0);
  93. }
  94.  
  95. void CMainWindow::OnOptionsAbout ()
  96. {
  97.     CAboutDialog dlg (this);
  98.     dlg.DoModal ();
  99. }
  100.  
  101. void CMainWindow::DoSolidFill (CDC* pDC, CRect* pRect)
  102. {
  103.     static COLORREF crColor[5] = {
  104.         RGB (255,   0,   0),
  105.         RGB (  0, 255,   0),
  106.         RGB (  0,   0, 255),
  107.         RGB (255,   0, 255),
  108.         RGB (  0, 255, 255),
  109.     };
  110.  
  111.     CBrush brush (crColor[m_nFillColor]);        
  112.     pDC->FillRect (pRect, &brush);
  113. }
  114.  
  115. void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
  116. {
  117.     CBrush* pBrush[64];
  118.     for (int i=0; i<64; i++) {
  119.         switch (m_nFillColor) {
  120.  
  121.         case 0:
  122.             pBrush[i] = new CBrush (RGB (255 - (i * 4), 0, 0));
  123.             break;
  124.  
  125.         case 1:
  126.             pBrush[i] = new CBrush (RGB (0, 255 - (i * 4), 0));
  127.             break;
  128.  
  129.         case 2:
  130.             pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
  131.             break;
  132.  
  133.         case 3:
  134.             pBrush[i] = new CBrush (RGB (255 - (i * 4), 0,
  135.                 255 - (i * 4)));
  136.             break;
  137.  
  138.         case 4:
  139.             pBrush[i] = new CBrush (RGB (0, 255 - (i * 4),
  140.                 255 - (i * 4)));
  141.             break;
  142.         }
  143.     }
  144.  
  145.     int nWidth = pRect->Width ();
  146.     int nHeight = pRect->Height ();
  147.     CRect rect;
  148.  
  149.     for (i=0; i<nHeight; i++) {
  150.         rect.SetRect (0, i, nWidth, i + 1);
  151.         pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
  152.     }
  153.  
  154.     for (i=0; i<64; i++)
  155.         delete pBrush[i];
  156. }
  157.  
  158. void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
  159. {
  160.     CFont font;
  161.     int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * m_nHeight) / 72);
  162.  
  163.     font.CreateFont (nHeight, 0, 0, 0, m_bBold ? FW_BOLD : FW_NORMAL,
  164.         m_bItalic, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
  165.         CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
  166.         FF_DONTCARE, "Times New Roman");
  167.  
  168.     pDC->SetBkMode (TRANSPARENT);
  169.     pDC->SetTextColor (RGB (255, 255, 255));
  170.  
  171.     CFont* pOldFont = pDC->SelectObject (&font);
  172.     pDC->DrawText (m_text, -1, pRect, DT_SINGLELINE | DT_CENTER |
  173.         DT_VCENTER);
  174.  
  175.     pDC->SelectObject (pOldFont);
  176. }
  177.  
  178. /////////////////////////////////////////////////////////////////////////
  179. // CSettingsDialog message map and member functions
  180.  
  181. BEGIN_MESSAGE_MAP (CSettingsDialog, CDialog)
  182.     ON_BN_CLICKED (IDC_DEFAULTS, OnDefaults)
  183. END_MESSAGE_MAP ()
  184.  
  185. void CSettingsDialog::OnDefaults ()
  186. {
  187.     m_nFillType = 1;
  188.     m_nFillColor = 2;
  189.     m_text = "Hello, MFC";
  190.     m_nHeight = 72;
  191.     m_bBold = TRUE;
  192.     m_bItalic = TRUE;
  193.  
  194.     UpdateData (FALSE);
  195. }
  196.  
  197. void CSettingsDialog::DoDataExchange (CDataExchange* pDX)
  198. {
  199.     CDialog::DoDataExchange (pDX);
  200.  
  201.     DDX_Radio (pDX, IDC_SOLID, m_nFillType);
  202.     DDX_Radio (pDX, IDC_RED, m_nFillColor);
  203.     DDX_Text (pDX, IDC_TEXT, m_text);
  204.     DDX_Text (pDX, IDC_HEIGHT, m_nHeight);
  205.     DDV_MinMaxInt (pDX, m_nHeight, 8, 144);
  206.     DDX_Check (pDX, IDC_BOLD, m_bBold);
  207.     DDX_Check (pDX, IDC_ITALIC, m_bItalic);
  208. }
  209.  
  210. /////////////////////////////////////////////////////////////////////////
  211. // CAboutDialog message map and member functions
  212.  
  213. BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
  214.     ON_WM_PAINT ()
  215. END_MESSAGE_MAP ()
  216.  
  217. BOOL CAboutDialog::OnInitDialog ()
  218. {
  219.     CDialog::OnInitDialog ();
  220.  
  221.     CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
  222.     pStatic->GetWindowRect (&m_rect);
  223.     pStatic->DestroyWindow ();
  224.     ScreenToClient (&m_rect);
  225.  
  226.     return TRUE;
  227. }
  228.  
  229. void CAboutDialog::OnPaint ()
  230. {
  231.     CPaintDC dc (this);
  232.     HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
  233.         GCL_HICON);
  234.  
  235.     if (hIcon != NULL) {
  236.         CDC dcMem;
  237.         dcMem.CreateCompatibleDC (&dc);
  238.  
  239.         CBitmap bitmap;
  240.         bitmap.CreateCompatibleBitmap (&dc, 32, 32);
  241.         CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
  242.  
  243.         CBrush brush (::GetSysColor (COLOR_3DFACE));
  244.         dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
  245.         dcMem.DrawIcon (0, 0, hIcon);
  246.  
  247.         dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
  248.             m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
  249.  
  250.         dcMem.SelectObject (pOldBitmap);
  251.     }
  252. }
  253.